package com.houseman.workFlow.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.houseman.workFlow.bean.TaskArea;
import com.houseman.workFlow.dao.TaskAreaDao;
import com.houseman.workFlow.dto.NextStepInfo;
import com.houseman.workFlow.utils.DBUtils;

public class TaskAreaDaoImpl implements TaskAreaDao{

	/**
	 * 根据graphId获得所有taskArea对象
	 * @return
	 * @throws Exception
	 */
	public List<TaskArea> queryTaskAreas(int graphId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT ID,GRAPHID,TID,X,Y,TEXT,TX,TY,APPROLE,PAGE,STEP,FLAG FROM TASKAREA WHERE GRAPHID=?");
		ps.setInt(1, graphId);
		
		ResultSet rs = ps.executeQuery();
		
		List<TaskArea> ts = new ArrayList<TaskArea>();
		while (rs.next()) {
			TaskArea ta = new TaskArea();
			ta.setId(rs.getInt("ID"));
			ta.setGraphId(rs.getInt("GRAPHID"));
			ta.setTid(rs.getInt("TID"));
			ta.setX(rs.getInt("X"));
			ta.setY(rs.getInt("Y"));
			ta.setText(rs.getString("TEXT"));
			ta.setTx(rs.getInt("TX"));
			ta.setTy(rs.getInt("TY"));
			ta.setAppRole(rs.getString("APPROLE"));
			ta.setPage(rs.getString("PAGE"));
			ta.setStep(rs.getInt("STEP"));
			ta.setFlag(rs.getInt("FLAG"));
			ts.add(ta);
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return ts;
	}
	
	/**
	 * 根据graphId查询所有taskArea的Id
	 * @return
	 * @throws Exception
	 */
	public List<Integer> queryTaskAreaIds(int graphId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT ID FROM TASKAREA WHERE GRAPHID=?");
		ps.setInt(1, graphId);
		
		ResultSet rs = ps.executeQuery();
		
		List<Integer> ids = new ArrayList<Integer>();
		while (rs.next()) {
			ids.add(rs.getInt("ID"));
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return ids;
	}
	
	/**
	 * 获取下一办理TaskArea集合
	 * @param step
	 * @return
	 * @throws Exception
	 */
	public List<NextStepInfo> queryNextStepInfo(int graphId, int step) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT R.ROLENAME,R.ROLEID,L.EXECID,A.FLAG,A.STEP"
							+ " FROM ROLE R RIGHT JOIN TASKAREA A ON R.ROLEID=A.APPROLE JOIN TASKAREA T JOIN LINE L"
							+ " WHERE A.TID=L.GROWTO AND A.GRAPHID=? AND L.TID=T.TID AND L.GRAPHID=T.GRAPHID AND T.STEP=? AND T.GRAPHID=?");
		ps.setInt(1, graphId);
		ps.setInt(2, step);
		ps.setInt(3, graphId);
		
		ResultSet rs = ps.executeQuery();
		
		List<NextStepInfo> infos = new ArrayList<NextStepInfo>();
		while (rs.next()) {
			NextStepInfo info = new NextStepInfo();
			
			info.setRoleName(rs.getString("ROLENAME"));
			info.setAppRole(rs.getString("ROLEID"));
			info.setExeCid(rs.getString("EXECID"));
			info.setFlag(rs.getInt("FLAG"));
			info.setStep(rs.getInt("STEP"));
			
			infos.add(info);
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return infos;
	}

	/**
	 * 获取当前办理TaskArea
	 * @param step
	 * @return
	 * @throws Exception
	 */
	public TaskArea queryTaskAreaByStep(int graphId, int step) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT ID,GRAPHID,TID,X,Y,TEXT,TX,TY,APPROLE,PAGE,STEP,FLAG FROM TASKAREA WHERE STEP=? AND GRAPHID=?");
		ps.setInt(1, step);
		ps.setInt(2, graphId);
		
		ResultSet rs = ps.executeQuery();
		
		TaskArea ta = new TaskArea();
		while (rs.next()) {
			ta.setId(rs.getInt("ID"));
			ta.setGraphId(rs.getInt("GRAPHID"));
			ta.setTid(rs.getInt("TID"));
			ta.setX(rs.getInt("X"));
			ta.setY(rs.getInt("Y"));
			ta.setText(rs.getString("TEXT"));
			ta.setTx(rs.getInt("TX"));
			ta.setTy(rs.getInt("TY"));
			ta.setAppRole(rs.getString("APPROLE"));
			ta.setPage(rs.getString("PAGE"));
			ta.setStep(rs.getInt("STEP"));
			ta.setFlag(rs.getInt("FLAG"));
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return ta;
	}
	
	/**
	 * 保存流程图信息
	 * @param graph
	 * @throws Exception
	 */
	public void savaTaskArea(List<TaskArea> tasks, int graphId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("INSERT INTO TASKAREA(GRAPHID,TID,X,Y,TEXT,TX,TY,APPROLE,PAGE,STEP,FLAG) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
		for (int i = 0; i < tasks.size(); i++) {
			ps.setInt(1, graphId);
			ps.setInt(2, tasks.get(i).getTid());
			ps.setInt(3, tasks.get(i).getX());
			ps.setInt(4, tasks.get(i).getY());
			ps.setString(5, tasks.get(i).getText());
			ps.setInt(6, tasks.get(i).getTx());
			ps.setInt(7, tasks.get(i).getTy());
			ps.setString(8, tasks.get(i).getAppRole());
			ps.setString(9, tasks.get(i).getPage());
			ps.setInt(10, tasks.get(i).getStep());
			ps.setInt(11, tasks.get(i).getFlag());
			
			ps.execute();
		}
		
		DBUtils.close(conn, ps);
	}
	
	/**
	 * 更新流程图信息
	 * @param graph
	 * @throws Exception
	 */
	public void updateTaskArea(List<TaskArea> tasks) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("UPDATE TASKAREA SET X=?,Y=?,TEXT=?,TX=?,TY=?,APPROLE=?,PAGE=?,STEP=? WHERE ID=?");
		for (int i = 0; i < tasks.size(); i++) {
			ps.setInt(1, tasks.get(i).getX());
			ps.setInt(2, tasks.get(i).getY());
			ps.setString(3, tasks.get(i).getText());
			ps.setInt(4, tasks.get(i).getTx());
			ps.setInt(5, tasks.get(i).getTy());
			ps.setString(6, tasks.get(i).getAppRole());
			ps.setString(7, tasks.get(i).getPage());
			ps.setInt(8, tasks.get(i).getStep());
			ps.setInt(9, tasks.get(i).getId());
			
			ps.executeUpdate();
		}
		
		DBUtils.close(conn, ps);
	}
	
	/**
	 * 删除流程图
	 * @param id
	 * @throws Exception
	 */
	public void deleteTaskAreaByGraphId(int graphId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("DELETE FROM TASKAREA WHERE GRAPHID=?");
		ps.setInt(1, graphId);
		
		ps.executeUpdate();
		
		DBUtils.close(conn, ps);
	}
	
	/**
	 * 删除流程图
	 * @param GRAPHID
	 * @throws Exception
	 */
	public void deleteTaskAreaById(List<Integer> ids) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("DELETE FROM TASKAREA WHERE ID=?");
		for (int i = 0; i < ids.size(); i++) {
			ps.setInt(1, ids.get(i));
			
			ps.executeUpdate();
		}
		
		DBUtils.close(conn, ps);
	}
}
